

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Fly542">
  <meta name="keywords" content="">
  
    <meta name="description" content="本文转自 http:&#x2F;&#x2F;jiangzhuti.me&#x2F;posts&#x2F;%E5%AD%97%E5%85%B8%E6%A0%91%E7%9A%84%E5%87%A0%E7%A7%8D%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F%E4%BB%A5%E5%8F%8A%E5%BA%94%E7%94%A8 字典树字典树(Trie)这一概念有时候会跟基数树(Radix Tree)、前缀树(">
<meta property="og:type" content="article">
<meta property="og:title" content="字典树的几种实现方式以及应用(转载)">
<meta property="og:url" content="http://fly542.cn/2023/11/29/00%E7%AE%97%E6%B3%95%E4%B8%8E%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/07%E5%AD%97%E5%85%B8%E6%A0%91%E7%9A%84%E5%87%A0%E7%A7%8D%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F%E4%BB%A5%E5%8F%8A%E5%BA%94%E7%94%A8(%E8%BD%AC%E8%BD%BD)/index.html">
<meta property="og:site_name" content="Fly542 技术沉淀">
<meta property="og:description" content="本文转自 http:&#x2F;&#x2F;jiangzhuti.me&#x2F;posts&#x2F;%E5%AD%97%E5%85%B8%E6%A0%91%E7%9A%84%E5%87%A0%E7%A7%8D%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F%E4%BB%A5%E5%8F%8A%E5%BA%94%E7%94%A8 字典树字典树(Trie)这一概念有时候会跟基数树(Radix Tree)、前缀树(">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://fly542.cn/2023/11/29/00%E7%AE%97%E6%B3%95%E4%B8%8E%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/07%E5%AD%97%E5%85%B8%E6%A0%91%E7%9A%84%E5%87%A0%E7%A7%8D%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F%E4%BB%A5%E5%8F%8A%E5%BA%94%E7%94%A8(%E8%BD%AC%E8%BD%BD)/trie-compress.png">
<meta property="og:image" content="http://fly542.cn/2023/11/29/00%E7%AE%97%E6%B3%95%E4%B8%8E%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/07%E5%AD%97%E5%85%B8%E6%A0%91%E7%9A%84%E5%87%A0%E7%A7%8D%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F%E4%BB%A5%E5%8F%8A%E5%BA%94%E7%94%A8(%E8%BD%AC%E8%BD%BD)/trie-dname.png">
<meta property="article:published_time" content="2023-11-29T06:28:41.000Z">
<meta property="article:modified_time" content="2023-12-19T08:59:07.632Z">
<meta property="article:author" content="Fly542">
<meta property="article:tag" content="linux">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="http://fly542.cn/2023/11/29/00%E7%AE%97%E6%B3%95%E4%B8%8E%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/07%E5%AD%97%E5%85%B8%E6%A0%91%E7%9A%84%E5%87%A0%E7%A7%8D%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F%E4%BB%A5%E5%8F%8A%E5%BA%94%E7%94%A8(%E8%BD%AC%E8%BD%BD)/trie-compress.png">
  
  
  <title>字典树的几种实现方式以及应用(转载) - Fly542 技术沉淀</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"fly542.cn","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 6.0.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Fly542 技术沉淀</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="字典树的几种实现方式以及应用(转载)">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2023-11-29 14:28" pubdate>
        2023年11月29日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      7k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      59 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">字典树的几种实现方式以及应用(转载)</h1>
            
            <div class="markdown-body">
              <p>本文转自 <a target="_blank" rel="noopener" href="http://jiangzhuti.me/posts/%E5%AD%97%E5%85%B8%E6%A0%91%E7%9A%84%E5%87%A0%E7%A7%8D%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F%E4%BB%A5%E5%8F%8A%E5%BA%94%E7%94%A8">http://jiangzhuti.me/posts/%E5%AD%97%E5%85%B8%E6%A0%91%E7%9A%84%E5%87%A0%E7%A7%8D%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F%E4%BB%A5%E5%8F%8A%E5%BA%94%E7%94%A8</a></p>
<h1 id="字典树"><a href="#字典树" class="headerlink" title="字典树"></a>字典树</h1><p>字典树(Trie)这一概念有时候会跟基数树(Radix Tree)、前缀树(Prefix Tree)等混用。其中前缀树、字典树是从字符串存储角度的称呼，基数树更多是从数值(多为二进制)角度的称呼，可以看作广义的字典树。以下以字典树来统称字典树和基数树。</p>
<p>关于字典树的介绍，前面的文章有一个直观的介绍，也不多赘述。</p>
<h2 id="功能对比"><a href="#功能对比" class="headerlink" title="功能对比"></a>功能对比</h2><table>
<thead>
<tr>
<th>数据结构</th>
<th>增加</th>
<th>删除</th>
<th>精确查找</th>
<th>极值查找</th>
<th>顺序遍历</th>
<th>前缀遍历</th>
<th>数据约束</th>
</tr>
</thead>
<tbody><tr>
<td>哈希表</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td></td>
<td></td>
<td></td>
<td>哈希函数;等于比较</td>
</tr>
<tr>
<td>堆</td>
<td>√</td>
<td>√</td>
<td></td>
<td>√</td>
<td></td>
<td></td>
<td>小于比较</td>
</tr>
<tr>
<td>平衡树</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>小于比较</td>
</tr>
<tr>
<td>字典树</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>二进制比较(字符比较)</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody></table>
<h2 id="基础版本字典树的特点："><a href="#基础版本字典树的特点：" class="headerlink" title="基础版本字典树的特点："></a>基础版本字典树的特点：</h2><ul>
<li>查询时间只和key长度有关，和树中的节点数量无关</li>
<li>设每个字符长度是s bits，一个key长度是k bits，最多只需要比较k / s个node</li>
</ul>
<h2 id="不足："><a href="#不足：" class="headerlink" title="不足："></a>不足：</h2><ul>
<li>基本只适用于基本数据(整数，字符串、二进制串)</li>
<li>很难像哈希表或者平衡树那样，通过自定义哈希函数或者比较函数来存储自定义类型</li>
</ul>
<h2 id="字典树的优化方向："><a href="#字典树的优化方向：" class="headerlink" title="字典树的优化方向："></a>字典树的优化方向：</h2><ul>
<li>减少内存overhead(减少节点大小、分支数量、节点层数)</li>
<li>缓存友好</li>
</ul>
<h1 id="常规优化"><a href="#常规优化" class="headerlink" title="常规优化"></a>常规优化</h1><p>还是主要参见前面介绍HAT的文章。主要有:</p>
<ul>
<li>字母表缩减。可以看作基数树换用更小的基数。代价是树的深度变大，速度慢。</li>
<li>使用关联容器。代价是每次索引运算量增大。</li>
<li>压缩字典树。把只有一个分叉的节点，连同其唯一的孩子，结合成一个节点。可以通过插入过程中分裂节点来实现：</li>
</ul>
<img src="/2023/11/29/00%E7%AE%97%E6%B3%95%E4%B8%8E%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/07%E5%AD%97%E5%85%B8%E6%A0%91%E7%9A%84%E5%87%A0%E7%A7%8D%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F%E4%BB%A5%E5%8F%8A%E5%BA%94%E7%94%A8(%E8%BD%AC%E8%BD%BD)/trie-compress.png" srcset="/img/loading.gif" lazyload class="">

<h1 id="PATRICIA-trie及其变体"><a href="#PATRICIA-trie及其变体" class="headerlink" title="PATRICIA trie及其变体"></a>PATRICIA trie及其变体</h1><h2 id="PATRICIA-trie"><a href="#PATRICIA-trie" class="headerlink" title="PATRICIA trie"></a>PATRICIA trie</h2><p>是一种特殊的字典树，每个中间节点记录key之间公共前缀的位置。从此位置之后，key之间产生不同，根据接下来的不同字符，转到不同的子分支。</p>
<h2 id="Critial-Bit-trie"><a href="#Critial-Bit-trie" class="headerlink" title="Critial Bit trie"></a>Critial Bit trie</h2><p>从基数树的角度看，PATRICIA trie最简单的一个实现是二进制的，即Critial Bit trie(cb trie)。critical bit的意思是两个串共同前缀分叉之处的比特。参见: <a target="_blank" rel="noopener" href="https://cr.yp.to/critbit.html">https://cr.yp.to/critbit.html</a> (这个作者对cb trie的吹捧，说实话有点过了)。</p>
<h3 id="两种节点类型："><a href="#两种节点类型：" class="headerlink" title="两种节点类型："></a>两种节点类型：</h3><ul>
<li>中间节点：存储critical-bit的位置，以及左右两个子树</li>
<li>叶子结点：存储整个串</li>
</ul>
<p>构造流程（采用网上某个介绍进行了修改）：</p>
<figure class="highlight prolog"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></div></td><td class="code"><pre><code class="hljs prolog">        empty   -- initial state<br><br>---------------------------------------------------<br><br>       <span class="hljs-number">01234</span>    -- number bit positions<br>insert <span class="hljs-number">01011</span>    -- the key<br><br><br>result root ----&gt; <span class="hljs-number">01011</span><br><br>---------------------------------------------------<br><br>insert <span class="hljs-number">01010</span><br>    search ends at <span class="hljs-number">01011</span>~=<span class="hljs-number">01010</span>;<br>    <span class="hljs-number">1</span>st difference is at position <span class="hljs-number">4</span>, so...<br><br>result root ----&gt; [<span class="hljs-number">4</span>]    -- i.e. test position #<span class="hljs-number">4</span><br>                 .   .<br>               <span class="hljs-number">0.</span>     <span class="hljs-number">.1</span><br>              .        .<br>           <span class="hljs-number">01010</span>     <span class="hljs-number">01001</span><br><br>---------------------------------------------------<br><br>insert <span class="hljs-number">10</span><br>    has no position #<span class="hljs-number">4</span>;<br>    can skip key positions but must test in order, so...<br><br>result root ----&gt; [<span class="hljs-number">0</span>]    -- i.e. test position #<span class="hljs-number">0</span><br>                 .   .<br>               <span class="hljs-number">0.</span>     <span class="hljs-number">.1</span><br>              .        .<br>           [<span class="hljs-number">4</span>]          <span class="hljs-number">10</span><br>          .   .<br>        <span class="hljs-number">0.</span>     <span class="hljs-number">.1</span><br>       .         .<br>     <span class="hljs-number">01010</span>      <span class="hljs-number">01011</span><br><br>---------------------------------------------------<br><br>insert <span class="hljs-number">000110</span>;<br>    search ends at <span class="hljs-number">01011</span>~=<span class="hljs-number">000110</span>;<br>    can skip key positions but must test in order, so...<br><br>result root ----&gt; [<span class="hljs-number">0</span>]<br>                 .   .<br>               <span class="hljs-number">0.</span>     <span class="hljs-number">.1</span><br>              .        .<br>           [<span class="hljs-number">1</span>]          <span class="hljs-number">10</span><br>          .   .<br>        <span class="hljs-number">0.</span>     <span class="hljs-number">.1</span><br>       .         .<br>    <span class="hljs-number">000110</span>       [<span class="hljs-number">4</span>]<br>                .   .<br>              <span class="hljs-number">0.</span>     <span class="hljs-number">.1</span><br>             .         .<br>            <span class="hljs-number">01010</span>     <span class="hljs-number">01011</span><br><br>---------------------------------------------------<br><br>insert <span class="hljs-number">01</span>;<br>    <span class="hljs-number">01</span> is also a prefix of <span class="hljs-number">01010</span> and <span class="hljs-number">01011</span>;<br>    must have ability to terminate at an intermediate node, as with <span class="hljs-symbol">Tries</span>.<br>result root ----&gt; [<span class="hljs-number">0</span>]<br>                 .   .<br>               <span class="hljs-number">0.</span>     <span class="hljs-number">.1</span><br>              .        .<br>           [<span class="hljs-number">1</span>]          <span class="hljs-number">10</span><br>          .   .<br>        <span class="hljs-number">0.</span>     <span class="hljs-number">.1</span><br>       .         .<br>    <span class="hljs-number">000110</span>       [<span class="hljs-number">2</span>] ---&gt; <span class="hljs-number">01</span><br>                  .<br>                 <span class="hljs-number">0.</span><br>                  .<br>                 [<span class="hljs-number">4</span>]<br>                .   .<br>              <span class="hljs-number">0.</span>      <span class="hljs-number">.1</span><br>             .          .<br>          <span class="hljs-number">01010</span>        <span class="hljs-number">01011</span><br></code></pre></td></tr></table></figure>
<h2 id="quelques-bits-popcount-trie"><a href="#quelques-bits-popcount-trie" class="headerlink" title="quelques-bits popcount trie"></a>quelques-bits popcount trie</h2><p>为了减少cb trie的深度，采用4比特一组进行比较。即，基数树以2的4次方等于16为基数。二叉树-&gt;16叉树。同时，采用了位运算来压缩数组，减少了节点大小。quelques-bits popcount trie简写为QP Trie. 参见： <a target="_blank" rel="noopener" href="https://dotat.at/prog/qp/README.html">https://dotat.at/prog/qp/README.html</a></p>
<p>构造过程和CB Trie类似：</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs rust">insert keys:<br><br><span class="hljs-string">&quot;foo&quot;</span> -&gt; hex <span class="hljs-built_in">str</span>: <span class="hljs-string">&quot;6 6 6 f 6 f&quot;</span><br><span class="hljs-string">&quot;bar&quot;</span> -&gt; hex <span class="hljs-built_in">str</span>: <span class="hljs-string">&quot;6 2 6 1 7 2&quot;</span><br><span class="hljs-string">&quot;baz&quot;</span> -&gt; hex <span class="hljs-built_in">str</span>: <span class="hljs-string">&quot;6 2 6 1 7 a&quot;</span><br><span class="hljs-string">&quot;hax&quot;</span> -&gt; hex <span class="hljs-built_in">str</span>: <span class="hljs-string">&quot;6 8 6 1 7 8&quot;</span><br><br>result root ----&gt;           [<span class="hljs-number">1</span>]<br>                          .  .  .<br>                        <span class="hljs-number">2</span>.  <span class="hljs-number">6</span>.   <span class="hljs-number">8</span>. ---&gt; <span class="hljs-string">&quot;hax&quot;</span><br>                        .    .<br>                       [<span class="hljs-number">5</span>]   . ---&gt; <span class="hljs-string">&quot;foo&quot;</span><br>                      .   .<br>                    <span class="hljs-number">2</span>.      .a<br>         <span class="hljs-string">&quot;bar&quot;</span> &lt;--- .         .---&gt; <span class="hljs-string">&quot;baz&quot;</span><br></code></pre></td></tr></table></figure>
<p>减少空间占用：用位图压缩子树数组</p>
<p>以此为例：</p>
<figure class="highlight excel"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs excel">                            [<span class="hljs-number">1</span>]<br>                          .  .  .<br>                        <span class="hljs-number">2</span>.  <span class="hljs-number">6</span>.   <span class="hljs-number">8</span>. ---&gt; <span class="hljs-string">&quot;hax&quot;</span><br><br>该节点保存有位图<span class="hljs-symbol">:</span><br>bitm<span class="hljs-symbol">ap:</span> <span class="hljs-number">0000000101000100</span><br>ind<span class="hljs-symbol">ex:</span>  fedcba9876543210<br><br>正常情况我们需要一个大小为<span class="hljs-number">16</span>的数组，其下标<span class="hljs-number">2</span>、<span class="hljs-number">6</span>、<span class="hljs-number">8</span>分别指向对应的子数。<br>我们压缩子树数组为：<br>vector[<span class="hljs-number">3</span>]<br>映射：<br>vector[<span class="hljs-number">0</span>] -&gt; &#x27;<span class="hljs-number">2</span>&#x27;所在子树<br>vector[<span class="hljs-number">1</span>] -&gt; &#x27;<span class="hljs-number">6</span>&#x27;所在子树<br>vector[<span class="hljs-number">2</span>] -&gt; &#x27;<span class="hljs-number">8</span>&#x27;所在子树<br><br><br>查询算法：<br>设要查询i位置的子树，<br><br>mask = <span class="hljs-number">1</span> &lt;&lt; i;<br><span class="hljs-built_in">if</span>(bitmap &amp; mask)<br>    member = vector[popcount(bitmap &amp; mask-<span class="hljs-number">1</span>)]<br><br>例如i = <span class="hljs-number">8</span>,<br><br>popcount(bitmap &amp; mask - <span class="hljs-number">1</span>) = popcount(<span class="hljs-number">0000000101000100</span> &amp; <span class="hljs-number">0000000001111111</span>)<br>= popcount(<span class="hljs-number">0000000001000100</span>)<br>= <span class="hljs-number">2</span><br><br>因此vector[<span class="hljs-number">2</span>]就是&#x27;<span class="hljs-number">8</span>&#x27;所在的子树<br></code></pre></td></tr></table></figure>

<h2 id="HAT-trie"><a href="#HAT-trie" class="headerlink" title="HAT trie"></a>HAT trie</h2><p>参见前面关于HAT trie的文章。</p>
<h2 id="Adaptive-Radix-Tree"><a href="#Adaptive-Radix-Tree" class="headerlink" title="Adaptive Radix Tree"></a>Adaptive Radix Tree</h2><p>自适应的基数树。Adaptive Radix Tree。参见: <a target="_blank" rel="noopener" href="https://db.in.tum.de/~leis/papers/ART.pdf">https://db.in.tum.de/~leis/papers/ART.pdf</a></p>
<p>存在4种不同类型和大小的节点。根据实际某个节点的子树数量，自适应的改变node类型：</p>
<figure class="highlight abnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs abnf">union Node &#123;<br>    Node4* n4<span class="hljs-comment">;</span><br>    Node16* n16<span class="hljs-comment">;</span><br>    Node48* n48<span class="hljs-comment">;</span><br>    Node256* n256<span class="hljs-comment">;</span><br>&#125;<br></code></pre></td></tr></table></figure>
<p>关于其如何存储k-v，这里不作介绍。</p>
<p>Node4<br>因为子树数量较少，直接for循环比较查找子树：</p>
<figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs crmsh">struct Node4 &#123;<br>    char child_keys[<span class="hljs-number">4</span>];<br>    <span class="hljs-keyword">Node</span><span class="hljs-title">* child_pointers</span>[<span class="hljs-number">4</span>];<br>&#125;<br><br><span class="hljs-keyword">Node</span><span class="hljs-title">* find_child</span>(char c, Node4* <span class="hljs-keyword">node</span><span class="hljs-title">) &#123;</span><br><span class="hljs-title">    Node</span>* ret = NULL;<br>    for (int i = <span class="hljs-number">0</span>; i <span class="hljs-tag">&lt; 4; ++i) &#123;</span><br><span class="hljs-tag">        if (child_keys[i] == c) ret = node-&gt;</span>child_pointers[i];<br>    &#125;<br><br>    return ret;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>Node16</p>
<figure class="highlight abnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs abnf">struct Node16 &#123;<br>    char child_keys[<span class="hljs-number">16</span>]<span class="hljs-comment">;</span><br>    Node* child_pointers[<span class="hljs-number">16</span>]<span class="hljs-comment">;</span><br>    byte num_children<span class="hljs-comment">;</span><br>&#125;<br></code></pre></td></tr></table></figure>
<p>可以采用SIMD进行并行加速查找：</p>
<figure class="highlight reasonml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs reasonml"><span class="hljs-comment">// Find the child in `node` that matches `c` by examining all child nodes, in parallel.</span><br>Node* find<span class="hljs-constructor">_child(<span class="hljs-params">char</span> <span class="hljs-params">c</span>, Node16<span class="hljs-operator">*</span> <span class="hljs-params">node</span>)</span> &#123;<br>    <span class="hljs-comment">// key_vec is 16 repeated copies of the searched-for byte, one for every possible position</span><br>    <span class="hljs-comment">// in child_keys that needs to be searched.</span><br>    __mm128i key_vec = <span class="hljs-constructor">_mm_set1_epi8(<span class="hljs-params">c</span>)</span>;<br><br>    <span class="hljs-comment">// Compare all child_keys to &#x27;c&#x27; in parallel. Don&#x27;t worry if some of the keys aren&#x27;t valid,</span><br>    <span class="hljs-comment">// we&#x27;ll mask the results to only consider the valid ones below.</span><br>    __mm128i results = <span class="hljs-constructor">_mm_cmpeq_epi8(<span class="hljs-params">key_vec</span>, <span class="hljs-params">node</span>-&gt;<span class="hljs-params">child_keys</span>)</span>;<br><br>    <span class="hljs-comment">// Build a mask to select only the first node-&gt;num_children values from the comparison</span><br>    <span class="hljs-comment">// (because the other values are meaningless)</span><br>    <span class="hljs-built_in">int</span> mask = (<span class="hljs-number">1</span> &lt;&lt; node-&gt;num_children) - <span class="hljs-number">1</span>;<br><br>    <span class="hljs-comment">// Change the results of the comparison into a bitfield, masking off any invalid comparisons.</span><br>    <span class="hljs-built_in">int</span> bitfield = <span class="hljs-constructor">_mm_movemask_epi8(<span class="hljs-params">results</span>)</span> &amp; mask;<br><br>    <span class="hljs-comment">// No match if there are no &#x27;1&#x27;s in the bitfield.</span><br>    <span class="hljs-keyword">if</span> (bitfield<span class="hljs-operator"> == </span><span class="hljs-number">0</span>) return NULL;<br><br>    <span class="hljs-comment">// Find the index of the first &#x27;1&#x27; in the bitfield by counting the leading zeros.</span><br>    <span class="hljs-built_in">int</span> idx = ctz(bitfield);<br><br>    return node-&gt;child_pointers<span class="hljs-literal">[<span class="hljs-identifier">idx</span>]</span>;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>Node48</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs scss">struct Node48 &#123;<br>    <span class="hljs-comment">// Indexed by the key value, i.e. the child pointer for &#x27;f&#x27;</span><br>    <span class="hljs-comment">// is at child_ptrs[child_ptr_indexes[&#x27;f&#x27;]]</span><br>    char child_ptr_indexes<span class="hljs-selector-attr">[256]</span>;<br><br>    Node* child_ptrs<span class="hljs-selector-attr">[48]</span>;<br>    char num_children;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>48个子树的数量，遍历等查找较慢，此时做了一个空间和查找速度的折中（可以对比Node256）。</p>
<figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs crmsh"><span class="hljs-keyword">Node</span><span class="hljs-title">* find_child</span>(char c, Node48* <span class="hljs-keyword">node</span><span class="hljs-title">) &#123;</span><br><span class="hljs-title">    int</span> idx = <span class="hljs-keyword">node</span><span class="hljs-title">-&gt;child_ptr_indexes</span>[c];<br>    if (idx == -<span class="hljs-number">1</span>) return NULL;<br><br>    return <span class="hljs-keyword">node</span><span class="hljs-title">-&gt;child_ptrs</span>[idx];<br>&#125;<br></code></pre></td></tr></table></figure>
<p>Node256<br>这个就是最原始的做法</p>
<figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs crmsh">struct Node256 &#123;<br>    <span class="hljs-keyword">Node</span><span class="hljs-title">* child_ptrs</span>[<span class="hljs-number">256</span>];<br>&#125;<br><br><span class="hljs-keyword">Node</span><span class="hljs-title">* find_child</span>(char c, Node256* <span class="hljs-keyword">node</span><span class="hljs-title">) &#123;</span><br><span class="hljs-title">    return</span> child_ptrs[c];<br>&#125;<br></code></pre></td></tr></table></figure>

<p>两种优化措施<br>两种优化：延迟展开、路径压缩，基本类似于最前面讲到的压缩字典树。</p>
<h1 id="字典树的应用"><a href="#字典树的应用" class="headerlink" title="字典树的应用"></a>字典树的应用</h1><p>字典树的应用也是非常广泛，这里举几个有代表性的例子，以后有时间会增加更多例子，以及对某些场景做进一步分析学习。</p>
<p>DNS(域名数据库、IP地址库)<br>DNS服务器中，通常有两个地方用到字典树：域名数据库和IP地址库。</p>
<p>互联网中的域名是一个层级结构的字符串，天生适合用字典树存储：</p>
<img src="/2023/11/29/00%E7%AE%97%E6%B3%95%E4%B8%8E%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/07%E5%AD%97%E5%85%B8%E6%A0%91%E7%9A%84%E5%87%A0%E7%A7%8D%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F%E4%BB%A5%E5%8F%8A%E5%BA%94%E7%94%A8(%E8%BD%AC%E8%BD%BD)/trie-dname.png" srcset="/img/loading.gif" lazyload class="">

<ul>
<li><p>在Knot DNS Server中，采用了QP Trie作为域名的存储方案：<a target="_blank" rel="noopener" href="https://github.com/CZ-NIC/knot/tree/master/src/contrib/qp-trie">https://github.com/CZ-NIC/knot/tree/master/src/contrib/qp-trie</a> (早些时候的版本采用的是HAT trie)。</p>
</li>
<li><p>IP地址库，一般由许多条IP地址段(CIDR), 位置(地理区域，服务商)的记录组成。在权威DNS服务器中，用作EDNS进行基于地理位置的解析(geoip); 在Cache DNS中，用于缓存各权威服务的地址段。</p>
</li>
<li><p>在Knot中采用的QP trie实现了权威服务的<a target="_blank" rel="noopener" href="https://github.com/CZ-NIC/knot/blob/master/src/knot/modules/geoip/geoip.c">geoip</a></p>
</li>
<li><p>在Unbound中，采用类似CB trie做了<a target="_blank" rel="noopener" href="https://github.com/NLnetLabs/unbound/blob/master/edns-subnet/addrtree.c">EDNS缓存</a></p>
</li>
<li><p>路由表<br>路由表的存储、查找算法，经过持续不断的优化，目前可以说五花八门。由于路由表的特性，基于字典树的实现是其中有代表性的一大类。目前linux系统可以查看到按字典树组织路由表：</p>
<figure class="highlight dns"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><code class="hljs dns">$ cat /proc/net/fib_trie<br>Main:<br>  +-- <span class="hljs-number">0.0.0.0</span>/<span class="hljs-number">0 3 0 5</span><br>     |-- <span class="hljs-number">0.0.0.0</span><br>        /<span class="hljs-number">0</span> universe UNICAST<br>     +-- <span class="hljs-number">127.0.0.0</span>/<span class="hljs-number">8 2 0 2</span><br>        +-- <span class="hljs-number">127.0.0.0</span>/<span class="hljs-number">31 1 0 0</span><br>           |-- <span class="hljs-number">127.0.0.0</span><br>              /<span class="hljs-number">32</span> link BROADCAST<br>              /<span class="hljs-number">8</span> host LOCAL<br>           |-- <span class="hljs-number">127.0.0.1</span><br>              /<span class="hljs-number">32</span> host LOCAL<br>        |-- <span class="hljs-number">127.255.255.255</span><br>           /<span class="hljs-number">32</span> link BROADCAST<br>     +-- <span class="hljs-number">192.168.2.0</span>/<span class="hljs-number">24 2 0 1</span><br>        |-- <span class="hljs-number">192.168.2.0</span><br>           /<span class="hljs-number">32</span> link BROADCAST<br>           /<span class="hljs-number">24</span> link UNICAST<br>        |-- <span class="hljs-number">192.168.2.110</span><br>           /<span class="hljs-number">32</span> host LOCAL<br>        |-- <span class="hljs-number">192.168.2.255</span><br>           /<span class="hljs-number">32</span> link BROADCAST<br>Local:<br>  +-- <span class="hljs-number">0.0.0.0</span>/<span class="hljs-number">0 3 0 5</span><br>     |-- <span class="hljs-number">0.0.0.0</span><br>        /<span class="hljs-number">0</span> universe UNICAST<br>     +-- <span class="hljs-number">127.0.0.0</span>/<span class="hljs-number">8 2 0 2</span><br>        +-- <span class="hljs-number">127.0.0.0</span>/<span class="hljs-number">31 1 0 0</span><br>           |-- <span class="hljs-number">127.0.0.0</span><br>              /<span class="hljs-number">32</span> link BROADCAST<br>              /<span class="hljs-number">8</span> host LOCAL<br>           |-- <span class="hljs-number">127.0.0.1</span><br>              /<span class="hljs-number">32</span> host LOCAL<br>        |-- <span class="hljs-number">127.255.255.255</span><br>           /<span class="hljs-number">32</span> link BROADCAST<br>     +-- <span class="hljs-number">192.168.2.0</span>/<span class="hljs-number">24 2 0 1</span><br>        |-- <span class="hljs-number">192.168.2.0</span><br>           /<span class="hljs-number">32</span> link BROADCAST<br>           /<span class="hljs-number">24</span> link UNICAST<br>        |-- <span class="hljs-number">192.168.2.110</span><br>           /<span class="hljs-number">32</span> host LOCAL<br>        |-- <span class="hljs-number">192.168.2.255</span><br>           /<span class="hljs-number">32</span> link BROADCAST<br></code></pre></td></tr></table></figure></li>
<li><p>linux IDR<br>使用字典树，维护整数id到指针的映射。</p>
</li>
</ul>
<p>IDR把每一个ID分级数据进行管理，每一级维护着ID的5位数据，这样就可以把IDR分为7级进行管理（5*7=35，维 护的数据大于32位），如下所示：</p>
<p><code>31 30 | 29 28 27 26 25 | 24 23 22 21 20 | 19 18 17 16 15 | 14 13 12 11 10 | 9 8 7 6 5 | 4 3 2 1 0</code></p>
<p>例如数据ID为<code>0B 10 11111 10011 00111 11001 100001 00001</code>，寻址如下</p>
<figure class="highlight maxima"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs maxima">第一级寻址 ary1[<span class="hljs-number">0b10</span>]得到第二级地址ary2[]<br>ary3 = ary2[<span class="hljs-number">0b11111</span>]<br>ary4 = ary3[<span class="hljs-number">0b10011</span>]<br>ary5 = ary4[<span class="hljs-number">0b00111</span>]<br>ary6 = ary5[<span class="hljs-number">0b11001</span>]<br>ary7 = ary6[<span class="hljs-number">0b100001</span>]<br>ary8 = ary7[<span class="hljs-number">0b00001</span>]<br></code></pre></td></tr></table></figure>
            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/devops/">devops</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/linux/">linux</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2023/12/06/04DevOps/30docker%E9%95%9C%E5%83%8F%E5%88%B6%E4%BD%9C%E5%B9%B6%E4%B8%8A%E4%BC%A0/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">docker镜像制作并上传</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2023/11/28/00%E7%AE%97%E6%B3%95%E4%B8%8E%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/06HAT-trie%E7%BC%93%E5%AD%98%E6%95%8F%E6%84%9F%E5%AD%97%E5%85%B8%E6%A0%91(%E8%BD%AC%E8%BD%BD)/">
                        <span class="hidden-mobile">HAT-trie,缓存敏感字典树(转载)</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
